﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MBSVO;
using System.Globalization;

namespace MBS.UserControls.MovieUserControl
{
    public partial class ViewMovieUserControl : System.Web.UI.UserControl
    {
        #region transfer
        MBSBLC.BLC movieblc = new MBSBLC.BLC();

        public delegate void RemoveMovieHandler();

        public event RemoveMovieHandler RemoveMovieSuccessful;
        public event RemoveMovieHandler RemoveMovieFailed;

        public delegate void UpdateMovieHandler();

        public event UpdateMovieHandler UpdateMovieSuccessful;
        public event UpdateMovieHandler UpdateMovieFailed;

        private bool actionresult;
        public bool Actionresult
        {
            get { return actionresult; }
            set { actionresult = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                MovieList1.LoadDropDownBox();
                LoadMovieDetail(MovieList1.GetSelectedValue());
            }

            MovieList1.MovieListChanged += new MovieList.MovieListHandler(MovieList1_MovieListChanged);
            
        }
        void MovieList1_MovieListChanged(int SelectedValue)
        {
            LoadMovieDetail(SelectedValue);
           
           
        }

        protected void UpdateMovieBtn_Click(object sender, EventArgs e)
        {

            actionresult = movieblc.UpdateMovie(MovieIDLabel.Text, MovieTitleTextBox.Text, DescriptionTextBox.Text, DurationTextBox.Text.ToString(), GenreTextBox.Text, CastTextBox.Text, RatingTextBox.Text.ToString(), ImageURLTextBox.Text, DateTime.Parse(ReleaseDateTextBox.Text, new CultureInfo("en-US")),txtTrailerURL.Text);

            

           
            if (actionresult)
            {
                UpdateMovieSuccessful();
            }

            else
            {
                UpdateMovieFailed();
            }
        }


        protected void DeleteMovieBtn_Click(object sender, EventArgs e)
        {
            actionresult = movieblc.DeleteMovie(MovieList1.GetSelectedValue());
            if (actionresult)
            {
                RemoveMovieSuccessful();
                MovieList1.LoadDropDownBox();
                LoadMovieDetail(MovieList1.GetSelectedValue());
            }

            else
            {
                RemoveMovieFailed();
            }
        }

        private void LoadMovieDetail(int SelectedValue)
        {
            MovieVO movieVO = movieblc.GetMovieByID(SelectedValue);
            MovieIDLabel.Text = "" + movieVO.MovieID;
            MovieTitleTextBox.Text = movieVO.MovieTitle;
            DescriptionTextBox.Text = movieVO.Description;
            DurationTextBox.Text = "" + movieVO.Duration;
            GenreTextBox.Text = movieVO.Genre;
            CastTextBox.Text = movieVO.Cast;
            RatingTextBox.Text = "" + movieVO.Rating;
            ImageURLTextBox.Text = movieVO.ImageURL;
            ReleaseDateTextBox.Text = movieVO.ReleaseDate.Date.ToString("dd-MMM-yyyy");
            txtTrailerURL.Text = movieVO.TrailerURL;
        }


        protected void releaseDateCalendar_SelectionChanged(object sender, EventArgs e)
        {

        }

        #endregion
    }
}